#!/bin/bash

TOOL_PATH=`readlink -f $0`
TOOL_PATH=`dirname $TOOL_PATH`

echo "TOOL_PATH=${TOOL_PATH}"

CONFIG_FIEL=${TOOL_PATH}/.config

if [ -f "${CONFIG_FIEL}" ]; then
    source ${CONFIG_FIEL}
else
    echo ".config file does not exist.Run 'menuconfig'"
    # exit -1
fi

BOOTLOADER_BASE=0x08000000
FIRMWARE_DEFAULT_OFFSET=0x10000

DEFAULT_CHIP=${CONFIG_DEFAULT_STM32_DEVICE}
FIRMWARE_BUILD_PATH=${CONFIG_FIRMWARE_BUILD_PATH}
FIRMWARE_BIN_PATH=${FIRMWARE_BUILD_PATH}/bin
BOOTLOADER_BUILD_PATH=${CONFIG_BOOTLOADER_BUILD_PATH}
BOOTLOADER_BIN_PATH=${BOOTLOADER_BUILD_PATH}/bin
FIRMWARE_OFFSET=${CONFIG_DEFAULT_FIRMWARE_OFFSET}
DEBUGER_TYPE=${CONFIG_DEFAULT_DEBUGER_DEVICE}
TOOL_PATH=${CONFIG_COMPILE_TOOL_PATH}
PROJECT_NAME=${CONFIG_DEFAULT_PROJECT_NAME}
DEBUGER_PATH=${CONFIG_DEFAULT_DEBUGER_PATH}
COMPILE_ENV=${CONFIG_DEFAULT_COMPILE_ENV}

echo "tool_path             => $TOOL_PATH"
echo "chip                  => $DEFAULT_CHIP"
echo "firmware_build_path   => $FIRMWARE_BUILD_PATH"
echo "bootloader_build_path => $BOOTLOADER_BUILD_PATH"
echo "firmware_bin_path     => $FIRMWARE_BIN_PATH"
echo "bootloader_bin_path   => $BOOTLOADER_BIN_PATH"
echo "debuger_type          => $DEBUGER_TYPE"
echo "project_name          => $PROJECT_NAME"
echo "firmware_offset       => $FIRMWARE_OFFSET"
echo "debuger_path          => $DEBUGER_PATH"
echo "compile_env           => $COMPILE_ENV"

if [ 1 != $# ]; then
    echo "parameters number error!"
    exit -1
elif [ 'boot' != $1 ] && [ 'app' != $1 ]; then
    echo "project parameters type error!"
    exit -2
else
    FIRMWARE_TYPE=$1
fi

if [ 'app' == ${FIRMWARE_TYPE} ]; then
    EXECUTABLE_FILE_PATH=${FIRMWARE_BIN_PATH}
    if [ -z ${FIRMWARE_OFFSET} ]; then
        DOWNLOAD_POS=$((${BOOTLOADER_BASE}+${FIRMWARE_DEFAULT_OFFSET}))
    else
        DOWNLOAD_POS=$((${BOOTLOADER_BASE}+${FIRMWARE_OFFSET}))
    fi
elif [ 'boot' == ${FIRMWARE_TYPE} ]; then
    DOWNLOAD_POS=${BOOTLOADER_BASE}
    EXECUTABLE_FILE_PATH=${BOOTLOADER_BIN_PATH}
else
    echo "fatal error!!!"
    exit -3
fi

FARMWARE_HEX=$(ls ${EXECUTABLE_FILE_PATH}/${PROJECT_NAME}*.hex)
FARMWARE_BIN=$(ls ${EXECUTABLE_FILE_PATH}/${PROJECT_NAME}*.bin)

echo "<===  Start downloading ${FIRMWARE_TYPE} to ${DOWNLOAD_POS}  ===> "
echo ""

if [ 'stlink' == ${DEBUGER_TYPE} ]; then
    echo "download ${FARMWARE_BIN} use ${DEBUGER_TYPE}"
    st-info --descr
    st-flash write ${FARMWARE_BIN} ${DOWNLOAD_POS}
elif [ 'jlink' == ${DEBUGER_TYPE} ]; then
    JLINK_SCRIPT_FILE=jlink_config.txt

    rm -f ${JLINK_SCRIPT_FILE}
    touch ${JLINK_SCRIPT_FILE}

    # config download order
    echo "si 1" >> ${JLINK_SCRIPT_FILE}              # choose swd download
    echo "speed 1000" >> ${JLINK_SCRIPT_FILE}
    echo "device ${DEFAULT_CHIP}" >> ${JLINK_SCRIPT_FILE}
    echo "r" >> ${JLINK_SCRIPT_FILE}
    echo "h" >> ${JLINK_SCRIPT_FILE}
    echo "loadfile ${FARMWARE_HEX}" >> ${JLINK_SCRIPT_FILE}
    echo "r" >> ${JLINK_SCRIPT_FILE}
    echo "g" >> ${JLINK_SCRIPT_FILE}
    echo "qc" >> ${JLINK_SCRIPT_FILE}

    # execute download operation
    if [ 'linux' == ${COMPILE_ENV} ]; then
        JLinkExe -commandfile ${JLINK_SCRIPT_FILE}
    elif [ 'win' == ${COMPILE_ENV} ]; then
        JLink -commandfile ${JLINK_SCRIPT_FILE}
    fi
    echo "jlink debuger!"
elif [ 'o-dap' == ${DEBUGER_TYPE} ]; then
    echo "download ${FARMWARE_HEX} use ${DEBUGER_TYPE}"
    openocd -f ${TOOL_PATH}/interface/cmsis-dap.cfg \
            -f ${TOOL_PATH}/target/stm32f4x.cfg     \
            -c init \
            -c halt \
            -c "program ${FARMWARE_HEX} verify" \
            -c reset \
            -c exit
else
    echo "debuger error!"
fi 

echo ""
echo "<===  Finish downloading ${FIRMWARE_TYPE}  ===>"

exit 0